package com.briup.jz.service.impl;

import com.briup.jz.bean.ProductCategory;
import com.briup.jz.bean.ProductCategoryExample;
import com.briup.jz.dao.ProductCategoryMapper;
import com.briup.jz.service.IProductCategoryService;
import com.briup.jz.utils.CustomerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class ProductCategoryImpl implements IProductCategoryService {

    @Autowired
    private ProductCategoryMapper productCategoryMapper;
    @Override
    public void saveOrUpdate(ProductCategory productCategory) throws CustomerException {
        //判断是否输入产品输入主键
        if (productCategory.getId()==null){
//如果未输入则插入新数据
            productCategoryMapper.insert(productCategory);
        }else {
//            如输入则为更改数据
            productCategoryMapper.updateByPrimaryKey(productCategory);
        }
    }

    @Override
    public void deleteById(Long id) throws CustomerException {
//        根据主键查找
       ProductCategory productCategory=productCategoryMapper.selectByPrimaryKey(id);
//       判断是否有该分类
       if (productCategory==null){
           throw  new CustomerException("没有该产品分类");
       }
//       执行删除操作
       productCategoryMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<ProductCategory> query(Long id) {
        ProductCategoryExample example=new ProductCategoryExample();
//        判断是否输入id主键值
        if (id!=null){
//            根据主键查询
            example.createCriteria().andIdEqualTo(id);
        }
        return productCategoryMapper.selectByExample(example);
    }
}
